<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/11/10
  Time: 11:57
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="fm" %>
<script src="webjars/jquery/3.1.1/jquery.min.js" charset="utf-8"></script>

<html>
<head>
    <title>添加航班</title>
    <style type="text/css">
        #add {
            position: relative;
            left: 450px;
            top: 20px;
        }

        h1 {
            position: relative;
            left: 140px;
        }
    </style>
</head>
<body>
<form id="add" action="/getAddData" method="post">
    <h1>航班添加页面</h1>
    <span>航班编号：</span>&nbsp<input type="text" name="flightNo" onblur="doRegister()"/>&nbsp<span id="errorTip"></span>
    <br>
    <br>
    <span>起飞城市：</span>
    <select name="ago" style="height: 25px;  color: #64696c;"><!-- width: 150px; -->
        <OPTION value="0">--请选择起飞城市--</OPTION>
        <c:forEach items="${city}" var="c">
            <option value="${c.id}">${c.cityName}</option>
        </c:forEach>
    </select>
    <span>起飞时间：</span><input type="text" name="departureTime"/>
    <br>
    <br>
    <span>到达城市：</span>
    <select name="arrive" style="height: 25px;  color: #64696c;"><!-- width: 150px; -->
        <OPTION value="0">--请选择起飞城市--</OPTION>
        <c:forEach items="${city}" var="c">
            <option value="${c.id}">${c.cityName}</option>
        </c:forEach>
    </select>
    <span>到达时间：</span><input type="text" name="arrivalTime"/>
    <br>
    <br>
    <input type="button" value="添加" onclick="getAdd()">
</form>
</body>
</html>
<script type="text/javascript">
    //验证航班编号是否重复
    function doRegister() {
        var flightNo = $("input[name=flightNo]").val();
        if (flightNo === "") {
            console.log("flightNo不能为空");
        } else {
            $.ajax({
                url: "/getFlightNo",
                type: "post",
                data: "flightNo=" + flightNo,  //序列化表单
                dataType: "json",
                success: function (data) {  //返回值data为{"result":"提交成功"}
                    if (data == 0) {
                        document.getElementById("errorTip").innerHTML = "航班编号可用";
                    } else {
                        document.getElementById("errorTip").innerHTML = "航班编号以存在，请重新输入";
                    }
                }
            })
        }
    }

    function getAdd() {
        //日期格式正则表达式
        var timeRegex = /^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\s((([0-1][0-9])|(2?[0-3]))\:([0-5]?[0-9])((\s)|(\:([0-5]?[0-9])))))?$/;
        var ago = $("select[name=ago]").val();
        var arrive = $("select[name=arrive]").val();
        var departureTime = $("input[name=departureTime]").val();
        var arrivalTime = $("input[name=arrivalTime]").val();
        if (ago == 0) {
            alert("请选择起飞城市");
        } else if (timeRegex.test(departureTime) && departureTime != "") {
            if (arrive == 0) {
                alert("请选择到达城市");
            } else if (ago == arrive) {
                alert("起飞城市和到达城市不能相同");
            } else if (timeRegex.test(arrivalTime) && arrivalTime != "") {
                document.getElementById("add").submit()
            } else {
                alert("到达时间格式错误")
            }
        } else {
            alert("起飞时间格式错误")
        }
    }
</script>
